package com.blockmeta.bbs.immodule.widget.chatboard;

import android.content.Context;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blockmeta.bbs.immodule.R;
import com.blockmeta.bbs.immodule.adapter.CircleMemberSelectedAdapter;
import com.blockmeta.bbs.immodule.widget.SideBar;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static com.blockmeta.bbs.immodule.Constants.CALL_CHAR;

public class PopupQueryList implements SideBar.OnTouchingLetterChangedListener
{
	public interface OnSelectedListener
	{
		public void onSelected(InputFieldQueryItem pItem);

		public void onCancel(String msg);
	}
	
	private InputFieldQueryPopup m_popup;
	private EditText m_edtSearch;
	private OnSelectedListener m_listener;
	private boolean m_bIsShowProcess = false;
	private List<InputFieldQueryItem> m_listItem = new ArrayList<>();
	private HashSet<InputFieldQueryItem> m_setItem = new HashSet<>();
	private ImageView m_imgClear;
	private View m_btnDone;

	RecyclerView mRcvCircleMember;
	TextView mGroupDialog;
	SideBar mSidebar;
	public CircleMemberSelectedAdapter mCircleMemberAdapter;

	public PopupQueryList(Context pContext, int pDefaultIconRes)
	{
		m_popup = new InputFieldQueryPopup(pContext, android.R.style.Theme_Translucent_NoTitleBar);
		FrameLayout tFrameLayout = new FrameLayout(pContext);

		LayoutInflater layoutInflater = LayoutInflater.from(pContext);
		View tView = layoutInflater.inflate(R.layout.share_control_input_popup, tFrameLayout);

		View tTitle = tView.findViewById(R.id.input_field_popup_title);

		m_edtSearch = (EditText) tTitle.findViewById(R.id.title_bar_search_edt_search);
		m_imgClear = (ImageView) tTitle.findViewById(R.id.title_bar_search_img_clear);
		
		tTitle.findViewById(R.id.title_bar_search_img_back).setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				mCircleMemberAdapter.clearSelect();
				m_popup.dismiss();
				if(m_listener != null){
					m_listener.onCancel(CALL_CHAR);
				}
			}
		});

		m_imgClear.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				m_edtSearch.setText("");
			}
		});
		initSearchEdt();
		
		m_btnDone = tView.findViewById(R.id.input_field_popup_btn_done);
		m_btnDone.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (m_listener != null)
				{
					List<InputFieldQueryItem> checkedItems = mCircleMemberAdapter.getCheckedItems();
					for (InputFieldQueryItem item : checkedItems)
					{
						m_listener.onSelected(item);
					}
				}
				mCircleMemberAdapter.clearSelect();
				m_popup.dismiss();
			}
		});

		mRcvCircleMember = tView.findViewById(R.id.input_field_popup_list);
		mSidebar = tView.findViewById(R.id.sidebar);
		mGroupDialog = tView.findViewById(R.id.group_dialog);

		mSidebar.setOnTouchingLetterChangedListener(this);
		mSidebar.bringToFront();
		mSidebar.setTextView(mGroupDialog);

		mRcvCircleMember.setLayoutManager(new LinearLayoutManager(pContext));
		((SimpleItemAnimator) mRcvCircleMember.getItemAnimator()).setSupportsChangeAnimations(false);
		mCircleMemberAdapter = new CircleMemberSelectedAdapter(R.layout.item_contacts_friends, m_listItem);
		mCircleMemberAdapter.bindToRecyclerView(mRcvCircleMember);
		mCircleMemberAdapter.setEditable(true, false);

		mCircleMemberAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
			@Override
			public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
				mCircleMemberAdapter.checkChange(position);
			}
		});

		m_popup.setModal();
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		m_popup.setContentView(tFrameLayout, params, Gravity.CENTER_HORIZONTAL);
	}


	private void initSearchEdt()
	{		
		m_edtSearch.addTextChangedListener(new TextWatcher()
		{
			private String lastValue = "";
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}

			@Override
			public void afterTextChanged(Editable s)
			{
				if (s.toString().equals(""))
				{
					m_imgClear.setVisibility(View.GONE);
				}
				else
				{
					m_imgClear.setVisibility(View.VISIBLE);
				}
				if (!m_bIsShowProcess)
					query(s.toString());
			}
			
		});
	}
	
	public void setOnSelectedListener(OnSelectedListener pListener)
	{
		m_listener = pListener;
	}
	
	public void show(String pTitleHint, HashSet<InputFieldQueryItem> pItems, InputFieldQueryAdapter.InputQueryItemMode pMode)
	{
		if (pItems == null)
		{
			return;
		}

		m_setItem = pItems;
		m_listItem.clear();
		for (InputFieldQueryItem pItem : pItems)
		{
			if (pItem.getKey() != null && pItem.getTitle() != null)
			{
				m_listItem.add(pItem);
			}
		}
		m_bIsShowProcess = true;
		m_edtSearch.setText("");
		m_edtSearch.setHint(pTitleHint);
		
		mCircleMemberAdapter.notifyDataSetChanged();
		m_bIsShowProcess = false;
		
		m_btnDone.setVisibility(View.VISIBLE);

		m_popup.show();
	}
	
	private void query(String pStr)
	{

		m_listItem.clear();

		for (InputFieldQueryItem pItem : m_setItem)
		{
			if (pItem.getKey() != null && pItem.getTitle() != null)
			{
				if (pStr.equals("") || pItem.getKey().startsWith(pStr) || pItem.getTitle().contains(pStr))
				{
					m_listItem.add(pItem);
				}
			}
		}
		mCircleMemberAdapter.clearSelect();
	}

	@Override
	public void onTouchingLetterChanged(String s) {
		mCircleMemberAdapter.scrollToTargetLetter(s);
	}
}
